package com.jf.hb.ui.interfaces;

import com.jf.hb.HB;
import com.jf.hb.ui.JFHandler;
import org.apache.commons.codec.digest.DigestUtils;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-08-08
 * Time:3:23
 */
public class Login extends Interface {
    private final String username;
    private final String password;

    private String un;
    private String pw;

    private final Font font1;//头
    private final Font font2;//体
    private final Font font3;//警告

    private final Box vBox;
    private Box passwordWarnBox;
    private Box usernameWarnBox;

    private final ImageIcon bk;
    public Login() {
        username = HB.username;
        password = HB.password;

        font1 = new Font("华文行楷", Font.PLAIN, 28);
        font2 = new Font("微软雅黑", Font.PLAIN, 20);
        font3 = new Font("微软雅黑", Font.PLAIN, 14);

        vBox = Box.createVerticalBox();

        bk = new ImageIcon(Objects.requireNonNull(getClass().getResource("/img/login.jpg")));

        init();
    }
    private void init() {
        initTitle();
        initUsernameField();
        initPasswordField();
        initButton();
        this.add(vBox);
    }
    private void initTitle() {
        Box titleBox = Box.createHorizontalBox();
        JLabel title = new JLabel("用户登录");
        title.setFont(new Font("微软雅黑", Font.BOLD, 42));
        title.setForeground(Color.BLACK);
        titleBox.add(title);
        vBox.add(Box.createVerticalStrut(70));
        vBox.add(titleBox);
    }
    private void initUsernameField() {
        Box usernameBox = Box.createHorizontalBox();
        usernameWarnBox = Box.createHorizontalBox();

        JLabel usernameJLabel = new JLabel("用户名:");
        usernameJLabel.setFont(font1);
        usernameJLabel.setForeground(Color.BLACK);

        JTextField usernameField = new JTextField(8);
        usernameField.setFont(font2);
        usernameField.setForeground(Color.BLACK);
        usernameField.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                if (usernameField.getText().length() > 10) {
                    new Thread(() -> {
                        usernameField.setText(un);
                        usernameField.setCaretPosition(usernameField.getText().length());
                    }).start();
                } else {
                    un = usernameField.getText();
                }
                usernameWarnBox.setVisible(false);
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                un = usernameField.getText();
                usernameWarnBox.setVisible(false);
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                un = usernameField.getText();
                usernameWarnBox.setVisible(false);
            }
        });

        JLabel usernameWarnJLabel = new JLabel("用户名不存在");
        usernameWarnJLabel.setFont(font3);
        usernameWarnJLabel.setForeground(Color.red);

        vBox.add(Box.createVerticalStrut(70));
        usernameBox.add(usernameJLabel);
        usernameBox.add(Box.createHorizontalStrut(33));
        usernameBox.add(usernameField);
        vBox.add(usernameBox);
        usernameWarnBox.add(Box.createHorizontalStrut(50));
        usernameWarnBox.add(usernameWarnJLabel);
        vBox.add(usernameWarnBox);

        usernameWarnBox.setVisible(false);
    }
    private void initPasswordField() {
        Box passwordBox = Box.createHorizontalBox();
        passwordWarnBox = Box.createHorizontalBox();

        JLabel passwordJLabel = new JLabel(" 密  码 :");
        passwordJLabel.setFont(font1);
        passwordJLabel.setForeground(Color.BLACK);

        JPasswordField passwordField = new JPasswordField();
        passwordField.setFont(font2);
        passwordField.setForeground(Color.BLACK);
        passwordField.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                pw = new String(passwordField.getPassword());
                passwordWarnBox.setVisible(false);
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                pw = new String(passwordField.getPassword());
                passwordWarnBox.setVisible(false);
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                pw = new String(passwordField.getPassword());
                passwordWarnBox.setVisible(false);
            }
        });

        JLabel passwordWarnJLabel = new JLabel("密码错误");
        passwordWarnJLabel.setFont(font3);
        passwordWarnJLabel.setForeground(Color.red);

        vBox.add(Box.createVerticalStrut(40));
        passwordBox.add(passwordJLabel);
        passwordBox.add(Box.createHorizontalStrut(33));
        passwordBox.add(passwordField);
        vBox.add(passwordBox);

        passwordWarnBox.add(Box.createHorizontalStrut(20));
        passwordWarnBox.add(passwordWarnJLabel);
        vBox.add(passwordWarnBox);

        passwordWarnBox.setVisible(false);
    }

    private void initButton() {
        Box buttonBox = Box.createHorizontalBox();

        JButton loginButton = new JButton("登录");
        loginButton.setFont(font2);
        loginButton.addActionListener(e -> {
            if (!username.equals(un)) {
                usernameWarnBox.setVisible(true);
            } else if (!password.equals(DigestUtils.md5Hex(pw))) {
                passwordWarnBox.setVisible(true);
            } else {
                JFHandler.switchSignal = JFHandler.IF.LOGIN;
            }
        });
        buttonBox.add(loginButton);
        vBox.add(Box.createVerticalStrut(40));
        vBox.add(buttonBox);
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(bk.getImage(), 0, 0, getWidth(), getHeight(), null);
    }

    @Override
    public void run() {

    }
}
